import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class myclient {
	
	private static BufferedReader in;
	private static PrintWriter out;
	
	

	public static void main(String[] args) throws UnknownHostException {

		String line;
		Socket socket;
		try {
			//Initialization variables
			String IP = null;
			int port = 80;
			String input = null;
			String filename = null;
			//Classification the input Command
			//GET case
			if (args[2].equals("GET")) {
				//Get IP, Port value and File name
				IP = args[0];
				port = Integer.valueOf(args[1]);
				filename = args[3];
				//Connecting to target sever through socket
				socket = new Socket(IP, port);
				
				//Use the bufferedReader of the InputSreamReader to read the massage from the server
				in = new BufferedReader(new InputStreamReader(socket
						.getInputStream()));
				//Use the PrintWriter of OutputStreamWriter to send the massage to the server
				out = new PrintWriter(new OutputStreamWriter(socket
						.getOutputStream()));
				//Send the certain commend line to the server (GET the server's HTML file)
				out.println("GET /" + filename + " HTTP/1.0\r\n\r\n");
				out.flush();
				
				//Use boolean variables to determine the infinite loop ended or not
				boolean end = false;

				//Use infinite loop to read the message(HTML content) from the server, and print out
				while (!end) {
					line = in.readLine();
					if (line != null) {
						System.out.println(line);
						//After Seeing the following Tag it will close the socket. That means the getting process is complete 
						if (line.equals("</html>")||line.equals("</script>")|| line.equals("404 NOT FOUND")||line.equals("</SCRIPT>")||line.equals("</HTML>") ) {
							end = true;
							socket.close();
						}
					}
				}
			//Put case
			} else if (args[2].equals("PUT")) {
				
				IP = args[0];
				port = Integer.valueOf(args[1]);
				filename = args[3];
				
				socket = new Socket(IP, port);
				in = new BufferedReader(new InputStreamReader(socket
						.getInputStream()));
				out = new PrintWriter(new OutputStreamWriter(socket
						.getOutputStream()));
				//Send the command to the server, including PUT command and the file name
				out.println("PUT " + filename);
				out.flush();

				boolean end = false;
				
				//Choose the certain file
				File myFile = new File("C://myclient/"
						+ filename);
				
				int fsize = (int) myFile.length();
				//Declare a byte array with 1024bytes
				byte[] mybytearray = new byte[1024];
				//Read the file by using the bufferedInputStream
				FileInputStream fis = new FileInputStream(myFile);
				BufferedInputStream bis = new BufferedInputStream(fis);
				OutputStream os = socket.getOutputStream();
				
				//Read the stream into the byte array, and every time when it reaches 1024 bytes, send it to the server
				//and then keep reading until finishing

				do {
					if (fsize > 1024) {
						bis.read(mybytearray, 0, 1024);
						os.write(mybytearray, 0, 1024);
					
					} else {
						bis.read(mybytearray, 0, fsize);
						os.write(mybytearray, 0, fsize);
						
					}
					fsize = fsize - 1024;
				} while (fsize > 0);

			
				//close the socket after getting the 200 OK message
				while (!end) {
					line = in.readLine();
					if (line == null) {

					} else if (line.equals("200 OK File Created")) {
						System.out.println(line);
						end = true;
						

					}
				}
				bis.close();
				fis.close();
				os.close();
				socket.close();

			} else {
				//other unknown command
				System.out.println("Wrong Command");
			}
			
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		
		
		
	}

}
